package com.vvlive.caibao.smartcity.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.bean.PersonalMeetingBean;
import com.vvlive.caibao.smartcity.utils.MeetingsController;
import com.vvlive.caibao.smartcity.view.SlideCtrlView;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/11/2.
 */
public class MeetingsCtrlAdapter extends BaseAdapter {

    private static final String TAG = "MeetingsCtrlAdapter";

    private LayoutInflater mInflater;
    private List<PersonalMeetingBean> mMeetings;
    private Context context;
    private MeetingCtrlListener mListener;
    private OnItemClickListener onItemClickListener;
    private SlideCtrlView.OnActionDownListener onActionDownListener;
    private SlideCtrlView.OnCtrlClickListener onCtrlClickListener;

    public MeetingsCtrlAdapter(Context context, List<PersonalMeetingBean> meetings) {
        mInflater = LayoutInflater.from(context);
        mMeetings = meetings;
        this.context = context;
        mListener = new MeetingCtrlListener();
    }

    @Override
    public int getCount() {
        return mMeetings != null ? mMeetings.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mMeetings.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_personal_meeting, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PersonalMeetingBean bean = (PersonalMeetingBean) getItem(position);
        holder.setTopic(getTopic(bean.getTopic()));
        holder.setRoom(getRoom(bean.getRoom()));
        holder.setTime(getTime(bean));

        holder.contentView.setData(position);
        if (onCtrlClickListener != null) {
            holder.contentView.setOnCtrlClickListener(onCtrlClickListener);
        }
        holder.contentView.setOnInfoClickListener(mListener);
        if (onActionDownListener != null) {
            holder.contentView.setOnActionDownListener(onActionDownListener);
        }

        return convertView;
    }

    /**
     * ViewHolder
     */
    final static class ViewHolder {

        public SlideCtrlView contentView;
        public TextView mTopic;
        public TextView mRoom;
        public TextView mTime;

        public ViewHolder(View contentView) {
            this.contentView = (SlideCtrlView) contentView;
            mTopic = find(R.id.p_meeting_theme);
            mRoom = find(R.id.p_meeting_address);
            mTime = find(R.id.p_meeting_time);
        }

        public void setTopic(String topic) {
            mTopic.setText(topic);
        }

        public void setRoom(String room) {
            mRoom.setText(room);
        }

        public void setTime(String time) {
            mTime.setText(time);
        }

        private <T extends View> T find(int id) {
            return (T) contentView.findViewById(id);
        }
    }

    final class MeetingCtrlListener implements SlideCtrlView.OnInfoClickListener {

        @Override
        public void onInfoClick(SlideCtrlView view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, getPosition(view));
            }
        }

        private int getPosition(SlideCtrlView view) {
            return view != null ? (int) view.getData() : -1;
        }
    }

    private void log(Object obj) {
        Log.i(TAG, obj.toString());
    }

    private String getTopic(String topic) {
        return "主题：" + topic;
    }

    private String getRoom(String room) {
        return "地点：" + room;
    }

    private String getTime(PersonalMeetingBean bean) {
        return "时间：" + MeetingsController.getStartDateTime(bean.getTime())
                + " ~ " + MeetingsController.getEndDateTime(bean.getTime());
    }

    public interface OnItemClickListener {
        void onItemClick(SlideCtrlView view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnActionDownListener(SlideCtrlView.OnActionDownListener actionDownListener) {
        this.onActionDownListener = actionDownListener;
    }

    public void setOnCtrlListener(SlideCtrlView.OnCtrlClickListener ctrlListener) {
        onCtrlClickListener = ctrlListener;
    }

}
